//The White Ambit, Rendering-Framework
//Copyright (C) 2009  Moritz Strickhausen

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.



#include "Win32WindowRunnable.h"

Win32WindowRunnable::Win32WindowRunnable(HINSTANCE a_hInstance, int a_nCmdShow)
{
	m_hInstance=a_hInstance;
	m_nCmdShow=a_nCmdShow;
	m_pWindow=new Win32Window();
}

Win32WindowRunnable::~Win32WindowRunnable(void)
{
	delete m_pWindow;
}

unsigned int Win32WindowRunnable::run()
{
	if( FAILED( m_pWindow->InitWindow( m_hInstance, m_nCmdShow ) ) )
		return -1;

	// Main message loop
	MSG message = {0};
	while( GetMessage( &message, NULL, 0, 0 ) && m_pWindow->getWindowLoopRunning())
	{
		TranslateMessage( &message );
		DispatchMessage( &message );
		Sleep(1);
	}
	int result=(int)message.wParam;
	return result;
};

HWND Win32WindowRunnable::waitForWindowHandle(void)
{
	//TODO: clean up this busy waiting mess
	HWND hWnd=NULL;
	while(!hWnd){
		Sleep(10);
		hWnd=m_pWindow->getWindowHandle();
	}
	return hWnd;
}
